<template>
  <div class="box">
    <div class="flex_between row">
      <div class="row_left Today">
        <p class="title">Today's Data</p>
        <div class="left_main flex_between">
          <basic-donut-chart :chartData="todayChartData"></basic-donut-chart>
          
          <div class="main_right">
            <div class="data_box">
              <p class="box_font">Alarm times</p>
              <p class="num">{{ pageData.today_data?.alarm_count }}</p>
              <el-progress :percentage="pageData.today_data?.alarm_count/pageData.device_chart?.active_count || 0" >&nbsp;</el-progress>
            </div>
            <div class="data_box">
              <p class="box_font">Alarm processed times</p>
              <p class="num">{{ pageData.today_data?.alarm_handle_count }}</p>
              <el-progress :percentage="pageData.today_data?.alarm_handle_count/pageData.today_data?.alarm_count || 0" >&nbsp;</el-progress>
            </div>
            <div class="data_box">
              <p class="box_font">Fault times</p>
              <p class="num">{{ pageData.today_data?.fault_count }}</p>
              <el-progress :percentage="pageData.today_data?.fault_count/pageData.device_chart?.active_count || 0" >&nbsp;</el-progress>
            </div>
            <div class="data_box">
              <p class="box_font">Fault processed times</p>
              <p class="num">{{ pageData.today_data?.fault_handle_count }}</p>
              <el-progress :percentage="pageData.today_data?.fault_handle_count/pageData.device_chart?.fault_count || 0" >&nbsp;</el-progress>
            </div>
          </div>
        </div>
      </div>
      <div class="row_right News">
        <p class="title">Latest News</p>
        <div class="right_main">
          <div class="new_box flex_start" v-for="(item, index) in newsList" :key="index">
            <el-icon :size="24" :color="'#419eff'"><Bell /></el-icon>
            <div class="context">
              <p class="title">{{item.title}}</p>
              <p class="time">{{item.time}}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="flex_between row ">
      <div class="row_left Device_status">
        <p class="title">Device status</p>
        <!-- 设备状态 -->
        <div class="left_main" style="margin-top:0;">
          <div class="box flex_center">
            <el-progress type="circle" :percentage="100" :color="'#ff0000'">{{pageData.device_chart?.active_count}}</el-progress>
            <p class="font">Active count</p>
          </div>
          <div class="box flex_center">
            <el-progress type="circle" :percentage="100" :color="'#989898'">{{pageData.device_chart?.offline}}</el-progress>
            <p class="font">Offline</p>
          </div>
          <div class="box flex_center">
            <el-progress type="circle" :percentage="100" :color="'#419eff'">{{pageData.device_chart?.online}}</el-progress>
            <p class="font">online</p>
          </div>
        </div>
      </div>
      <div class="row_right Device">
        <p class="title">Device</p>
        <div class="right_main">
          <div class="box flex_center" v-for="(item, index) in deviceList" :key="index">
            <el-icon :size="40" :color="'#419eff'"><Bell /></el-icon>
            <p class="name">{{item.name}}</p>
          </div>
        </div>
      </div>
    </div>
    <div class="flex_between row"  style="height:35%">
      <div class="row_left Device">
        <p class="title">Device Status Curve</p>
        <daily-chart :echartData="dailyData"></daily-chart>
      </div>
      <div class="row_right Shortcut">
        <p class="title">Shortcut Tool</p>
        <div class="right_main">
          <span class="btn" v-for="item in shortoutList" :key="item.label" @click="toPage(item.name)">{{item.label}}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import API from "api";
import BasicDonutChart from '@/components/Charts/basicDonutChart.vue';
import dailyChart from "@/components/Charts/deviceDailyStatus.vue";

export default {
  name: 'index',

  components: {
    BasicDonutChart,
    dailyChart,
  },
  props: {
    alarm: {
      type: Boolean,
      default: () => false,
    },
  },
  emits: ["changeAlarm"],
  data() {
    return {
      pageData:{},
      todayChartData: [],
      shortoutList: [{
        label: 'Device',
        name: 'DeviceList'
      },{
        label: 'Tasks',
        name: 'Tasks'
      },{
        label: 'Role',
        name: 'Role'
      },{
        label: 'Config',
        name: 'Config'
      }],
      // newsList:[],
      newsList:[{
        title: 'Safety Guard App V1.0.0',
        time: '2022-10-11 19:05:48'
      },{
        title: 'Center Monitoring System V1.0',
        time: '2022-10-11 10:12:48'
      },{
        title: 'Safety Guard App V1.0.1',
        time: '2022-11-11 12:05:48'
      },{
        title: 'Safety Guard App V1.0.1',
        time: '2022-12-11 10:05:48'
      },{
        title: 'Safety Guard App published in appstore',
        time: '2023-02-11 19:00:48'
      },{
        title: 'Safety Guard App published in google play store',
        time: '2023-02-11 18:05:48'
      },{
        title: 'Center Monitoring System V1.0.1',
        time: '2023-03-11 20:05:48'
      }],
      deviceList: [{
        icon: '',
        name: 'Smart door sensor'
      },{
        icon: '',
        name: 'PIR motion sensor '
      },{
        icon: '',
        name: 'Wireless smoke detector'
      },{
        icon: '',
        name: 'SOS Panic button'
      },{
        icon: '',
        name: 'Gas leakage sensor'
      },{
        icon: '',
        name: 'Wireless smoke detector'
      },{
        icon: '',
        name: 'Gas leakage sensor'
      },{
        icon: '',
        name: 'PIR motion sensor'
      }],
      dailyData: {}
    }
  },
  
  mounted() {
    this.init()
    // const eventBus = useEventbus();
    // eventBus.emitterOn("AlarmMessage", (e) => {
    //   console.log('eee==', e, that.pageData)
    //   that.pageData.today_data.alarm_count += 1
    // });
  },
  watch: {
    alarm(val) {
        if (val) {
          this.pageData.today_data.alarm_count += 1
          this.$emit('changeAlarm', false)
        }
    },
  },
  methods:{
    
    init() {
      
      API.getSummarizeData().then(res => {
        this.pageData = res.data
        this.dailyData = res.data.last_week_chart
        this.dailyData.fontColor = '#000'

        this.todayChartData.push({
          value: res.data.today_data.alarm_count,
          name: 'Alarm count'
        },{
          value: res.data.today_data.alarm_handle_count,
          name: 'Alarm handle count'
        })
      })
    },
    toPage(name) {
      this.$router.push({name: name})
    }
  },

}


</script>

<style lang="scss" scoped>
:deep(.el-progress-circle) {
  svg {
    width: 100%;
    height: 100%;
  }
}
:deep(.el-progress-bar__outer) {
  background-color: #989898;
}
:deep(.el-progress-circle) {
  height: 105px!important;
}
.box {
  width: 100%;
  height: 100%;
  .row {
    box-sizing: border-box;
    width: 100%;
    height: 30%;
    padding: 20px;

    .row_left,
    .row_right {
      position: relative;
      box-sizing: border-box;
      padding: 20px;
      border-radius: 25px;
      box-shadow: 1px 2px 5px #3b3b3b;
    }
    .row_left {
      width: 69%;
      height: 100%;
    }
    .row_right {
      width: 29%;
      height: 100%;
    }
    .title {
      font-size: 20px;
      font-weight: 500;
    }

    .left_main {
      margin-top: -14px;
      .main_right {
        width: 70%;
        margin-top: -14px;
        .box_font {
          line-height: 1.2;
          font-size: 15px;
        }
        .num {
          font-size: 20px;
          line-height: 1.2;
          padding-bottom: 5px;
        }
        &>div{
          display: inline-block;
          width: 42%;
        }
      }
    }

    .right_main{
      display: flex;
      align-items: center;
      flex-wrap: wrap;
      justify-content: space-around;

      .btn {
        box-sizing: border-box;
        border-radius: 5px;
        width: 107px;
        height: 50px;
        line-height: 50px;
        text-align: center;
        background-color: rgba($color: #419EFF, $alpha: .5);
        font-size: 18px;
        color: #419EFF;
        margin-top: 20px;
        cursor: pointer;
      }
    }

    .Device_status {
      .left_main {
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-evenly;
        height: 92%;
        padding-bottom: 15px;
        .box {
          width: 30%;
          margin-top: 20px;
          .font {
            width: 100%;
            text-align: center;
            font-size: 16px;
          }
        }
      }
    }

    .News {
      .right_main {
        box-sizing: border-box;
        padding-top: 20px;
        height: 92%;
        overflow-y: scroll;

        .new_box {
          width: 100%;
          margin: 10px 0;
          .context {
            width: 80%;
            margin-left: 10px;
          }
          .title {
            width: 100%;
            white-space:nowrap;
            text-overflow:ellipsis;
            overflow:hidden;
            font-size: 14px;
          }
          .time {
            font-size: 11px;
          }
        }
      }
    }

    .Device  {
      .right_main {
        box-sizing: border-box;
        padding-top: 20px;
        height: 92%;
        overflow-y: scroll;
        justify-content: flex-start;

        .box {
          width: 30%;
          height: 90px;
          margin-top: 20px;

          .name {
            font-size: 10px;
            text-align: center;
          }
        }
      }
    }
  }
}
</style>
